<template>

  <!-- 新增部门的弹层 -->
  <el-dialog
    title="新增部门"
    :visible="showDialog"
    @close="handleCloseDialog"
    @open="openFn"
  >
    <el-form ref="addForm" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择">
          <el-option
            v-for="item in employeeList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" size="small" @click="confirm">确定</el-button>
      <el-button size="small" @click="handleCloseDialog">取消</el-button>
    </template>
  </el-dialog>

</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  name: 'AddDepartment',
  // 需要传入一个props变量来控制 显示或者隐藏
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    curId: {
      type: String,
      default: ''
    },
    allList: {
      type: Array,
      default() {
        return []
      }
    }
  },

  data() {
    const validateCode = (rule, value, callback) => {
      // 1. 拿到所有的原始节点数据  allList
      // 2. allList里面每一项的code码拿到组成一个新数组
      let list = []
      if (this.form.id) {
        // 编辑状态
        list = this.allList.filter(item => item !== this.cacheCode)
      } else {
        // 新增状态
        list = this.allList
      }

      if (list.includes(value)) {
        return callback(new Error('当前code码重复'))
      } else {
        callback()
      }
    }

    return {

      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validateCode, message: '部门编码不能出现重复', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ] },
      employeeList: [], // 员工列表
      cacheCode: null // 把当前要编辑的code单独缓存下来将来作为判断依据

    }
  },

  methods: {
    // 关闭弹框
    handleCloseDialog() {
      this.$emit('close-dialog', false)

      // 重置表单，清除校验结果
      this.$refs.addForm.resetFields()
    },
    // 员工列表

    async hGetEmployeeSimple() {
      const res = await getEmployeeSimple()
      this.employeeList = res
    },
    openFn() {
      this.hGetEmployeeSimple()
    },
    // 提交表单条用接口
    confirm() {
      // 兜底校验
      this.$refs.addForm.validate(async(valid) => {
        if (valid) {
          // 新增接口
          if (this.form.id) {
            // todo edit
            await updateDepartments(this.form)
          } else {
            // 2. 调用新增接口
            await addDepartments({ ...this.form, pid: this.curId })
          }
          this.$emit('upDate')
          // 关闭弹框
          this.handleCloseDialog()
        }
      })
    },
    // 详情数据拉取 搞定id 方法调用
    async getDetail(id) {
      // 调用接口 回填数据
      const res = await getDepartDetail(id)
      this.cacheCode = res.code // 单独缓存
      this.form = res
    }
  }
}
</script>

  <style  scoped>

  </style>
